package com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mlz.mlzmall.R;
import com.mlz.mlzmall.ui.utils.util.LogUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.text.SimpleDateFormat;

import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.GetMyPromotionResponse.DataBean.ListBean;

import java.util.Date;
import java.util.List;

/**
 * @description
 * Created by 安帅 on 2016/5/27 17:41.
 * PackageName com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.
 * ProjectName mlzmall_android.
 */
public class MyActivityAdapter extends BaseAdapter {
    private final int layout_id;
    private List<ListBean> list;
    private Context context;
    private LayoutInflater layoutInflater;

    public MyActivityAdapter(Context context, List<ListBean> promotionResponseList, int list_item_activity) {
        this.context = context;
        this.list = promotionResponseList;
        this.layout_id = list_item_activity;
//        LogUtils.d("adapter " + list.get(0).getDateAdded());
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(layout_id, null);
            holder.ivImage = (ImageView) convertView.findViewById(R.id.iv_image);
            holder.ivGo = (ImageView) convertView.findViewById(R.id.iv_go);
            holder.ivOk = (ImageView) convertView.findViewById(R.id.iv_ok);
            holder.ivOver = (ImageView) convertView.findViewById(R.id.iv_over);
            holder.ivFinishChange = (ImageView) convertView.findViewById(R.id.iv_finish_change);
            holder.tvTitleUser = (TextView) convertView.findViewById(R.id.tv_title_user);
            holder.tvDescrib = (TextView) convertView.findViewById(R.id.tv_describ);
            holder.tvNull = (TextView) convertView.findViewById(R.id.tv_null);
            holder.tvOver = (TextView) convertView.findViewById(R.id.tv_over);
            holder.tvDateEnd = (TextView) convertView.findViewById(R.id.tv_dateEnd);
            holder.tvOf = (TextView) convertView.findViewById(R.id.tv_of);
            holder.tvDateStart = (TextView) convertView.findViewById(R.id.tv_dateStart);
            holder.tvPromotionId = (TextView) convertView.findViewById(R.id.tv_promotionId);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (list.get(position).getType() == 1) {
            if (list.get(position).getState() == 1) {
                holder.ivFinishChange.setVisibility(View.VISIBLE);
            } else if (list.get(position).getState() == 2 && list.get(position).getState() == 3) {
                holder.ivGo.setVisibility(View.VISIBLE);
            } else if (list.get(position).getState() == 4) {
                holder.ivOk.setVisibility(View.VISIBLE);
            } else if (list.get(position).getState() == 5) {
                holder.ivFinishChange.setVisibility(View.VISIBLE);
            } else {
                holder.ivOver.setVisibility(View.VISIBLE);
            }
        } else if (list.get(position).getType() == 2) {
            if (list.get(position).getState() == 1) {
                holder.ivOver.setVisibility(View.VISIBLE);
            } else if (list.get(position).getState() == 2 || list.get(position).getState() == 3 || list.get(position).getState() == 4) {
                holder.ivGo.setVisibility(View.VISIBLE);
            } else if (list.get(position).getState() == 5) {
                holder.ivOk.setVisibility(View.VISIBLE);
            } else if (list.get(position).getState() == 6 || list.get(position).getState() == 7) {
                holder.ivFinishChange.setVisibility(View.VISIBLE);
            } else {
                holder.ivOver.setVisibility(View.VISIBLE);
            }
        }

        ImageLoader.getInstance().displayImage(list.get(position).getImage(), holder.ivImage);
        holder.tvTitleUser.setText(list.get(position).getName());
        holder.tvDescrib.setText(list.get(position).getDescribe());
        LogUtils.d("qweqweqwe:" + list.get(position).getName());
        //将后台的long型开始时间转换为正常日期并显示
        Date dat_start = new Date(list.get(position).getDateStart());
        java.util.GregorianCalendar gc = new java.util.GregorianCalendar();
        gc.setTime(dat_start);
        SimpleDateFormat formatstart = new SimpleDateFormat("yyyy.MM.dd");
        String sb = formatstart.format(gc.getTime());
        holder.tvDateStart.setText(sb);

        //将后台的long型结束时间转换为正常日期并显示
        Date dat_end = new Date(list.get(position).getDateEnd());
        java.util.GregorianCalendar ge = new java.util.GregorianCalendar();
        ge.setTime(dat_end);
        SimpleDateFormat formatend = new SimpleDateFormat("yyyy.MM.dd");
        String end = formatend.format(ge.getTime());
        holder.tvDateEnd.setText(end);
        return convertView;
    }

    class ViewHolder {
        ImageView ivImage;
        ImageView ivGo;
        ImageView ivOk;
        ImageView ivOver;
        ImageView ivFinishChange;
        TextView tvTitleUser;
        TextView tvDescrib;
        TextView tvNull;
        TextView tvOver;
        TextView tvDateEnd;
        TextView tvOf;
        TextView tvDateStart;
        TextView tvPromotionId;
    }
}
